<Project>
  <PropertyGroup>
    <NativeOptimizationDataSupported Condition="'$(TargetOS)' == 'windows' And ('$(TargetArchitecture)' == 'x64' Or '$(TargetArchitecture)' == 'x86' Or '$(TargetArchitecture)' == 'arm64')">true</NativeOptimizationDataSupported>
    <NativeOptimizationDataSupported Condition="'$(TargetOS)' == 'linux' And ('$(TargetArchitecture)' == 'x64' Or '$(TargetArchitecture)' == 'arm64')">true</NativeOptimizationDataSupported>
    <NativeOptimizationDataSupported Condition="'$(NoPgoOptimize)' == 'true'">false</NativeOptimizationDataSupported>
    <NativeOptimizationDataSupported Condition="'$(Configuration)' != 'Release'">false</NativeOptimizationDataSupported>

    <_NativeOptimizationDataPackageTarget>$(TargetOS)-$(TargetArchitecture.ToLower())</_NativeOptimizationDataPackageTarget>
    <_NativeOptimizationDataPackageTarget Condition="'$(TargetOS)' == 'windows'">windows_nt-$(TargetArchitecture.ToLower())</_NativeOptimizationDataPackageTarget>

  </PropertyGroup>
  <ItemGroup Condition="'$(optimizationPGOCoreCLRVersion)'!='' and '$(DotNetBuildSourceOnly)' != 'true'">
    <PackageReference Include="optimization.windows_nt-x64.PGO.CoreCLR"
      Version="$(optimizationPGOCoreCLRVersion)"
      GeneratePathProperty="true" />
    <PackageReference Include="optimization.windows_nt-x86.PGO.CoreCLR"
      Version="$(optimizationPGOCoreCLRVersion)"
      GeneratePathProperty="true" />
    <PackageReference Include="optimization.windows_nt-arm64.PGO.CoreCLR"
      Version="$(optimizationPGOCoreCLRVersion)"
      GeneratePathProperty="true" />
    <PackageReference Include="optimization.linux-x64.PGO.CoreCLR"
      Version="$(optimizationPGOCoreCLRVersion)"
      GeneratePathProperty="true" />
    <PackageReference Include="optimization.linux-arm64.PGO.CoreCLR"
      Version="$(optimizationPGOCoreCLRVersion)"
      GeneratePathProperty="true" />
  </ItemGroup>


  <!--                                                                       -->
  <!-- Task: GetPgoDataPackagePath                                           -->
  <!--                                                                       -->
  <!-- Notes:                                                                -->
  <!--                                                                       -->
  <!-- DumpPgoDataPackagePath is used to get the path of the native PGO data -->
  <!-- for other MSBuild projects, generally to pass to another project or   -->
  <!-- native script like build-runtime.cmd/sh.                              -->
  <!--                                                                       -->

  <Target Name="GetPgoDataPackagePath" Returns="$(PgoPackagePath)">
    <PropertyGroup>
      <PgoPackagePathProperty>Pkgoptimization_$(_NativeOptimizationDataPackageTarget)_PGO_CoreCLR</PgoPackagePathProperty>
    </PropertyGroup>

    <!--
      Use an item group for expansion of $($(PgoPackagePathProperty)) (an illegal MSBuild expression)
      i.e. the prop value's value.
    -->
    <ItemGroup>
      <PgoPackagePathPropertyItemList Include="$(PgoPackagePathProperty)" />
      <PgoPackagePathPropertyItemList>
        <PgoPackagePath>$(%(Identity).TrimEnd('\\'))</PgoPackagePath>
      </PgoPackagePathPropertyItemList>
    </ItemGroup>

    <PropertyGroup>
      <PgoPackagePath>@(PgoPackagePathPropertyItemList->'%(PgoPackagePath)')</PgoPackagePath>
    </PropertyGroup>

    <Error Condition="!Exists('$(PgoPackagePath)') And '$(NativeOptimizationDataSupported)' == 'true'" Text="Unable to locate restored PGO package at $(PgoPackagePath). Maybe the platform-specific package naming changed?" />
  </Target>

  <Target Name="OutputPgoPathForCI" Condition="'$(ContinuousIntegrationBuild)' == 'true' and '$(NativeOptimizationDataSupported)' == 'true'" DependsOnTargets="GetPgoDataPackagePath">
    <Message Text="##vso[task.setvariable variable=CoreClrPgoDataArg]-pgodatapath &quot;$(PgoPackagePath)&quot;" Importance="High" />
  </Target>
</Project>
